import { useIntersectionObserver, useIntervalFn } from '@vueuse/core'
import { ref,onBeforeMount } from 'vue'

export const lazyLoad = (callback: () => void) => {
  const target = ref(null)
  const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
    if (isIntersecting) {
      callback()
      stop()
    }
  })
  return target
}
//倒计时
export const useCountDown = (count: number = 60) => {
  const time = ref(0)
  const { resume, pause } = useIntervalFn(() => {
    if (time.value <= 0) {
      return pause()
    }
    time.value--
  }, 1000, { immediate: false })

  // 卸载组件也清除
  onBeforeMount(() => {
    pause()
  })
  const start = (num?: number) => {
    time.value = num || count
    resume()
  }
  return {
    start, time, resume
  }
}
